<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soa2Logs
 *
 * @author dxx
 */

namespace app\common\model\log;

use think\Model;
use app\common\common\Helper;

class soa2Logs extends Model {

    protected $table = 'soa2_logs';
    private $_baseTable = 'soa2_logs';
    protected $connection = ['database' => 'jooj_log'];
    
     public function __construct($data = array()) {
        $date = date('Ymd', time());
        $this->table = $this->_baseTable . '_' . $date;
        parent::__construct($data);
    } 
    
    private function _createTable() {
        $sql = 'CREATE TABLE ' . $this->table . ' LIKE ' . $this->_baseTable;
        return $this->execute($sql);
    }
    
    public function addLog($inPut, $outPut,$apiUserInfo) {
        
        $apiUserId = $apiUserInfo['id'];

        $isSuccess = ($outPut['code'] == '1') ? 1 : 0;

        $data = [
            'url' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
            'param' => $inPut,
            'result' => json_encode($outPut),
            'api_user_id' => $apiUserId,
            'log_time' => time(),
            'is_success' => $isSuccess,
            'response_time'=> microtime(true) - Helper::$requestTime
        ];
        \think\Cache::select(6);
        \think\Cache::lpush('data_logs', serialize(['table' => ['db' => 'jooj_log', 'name' => 'soa2_logs', 'sub' => 1], 'project' => 'soa2log', 'logstore' => 'soa2_logs', 'dataLogs' => $data]));
    }

}
